package com.zhousb.deviceManager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhousb.deviceManager.exception.CustomerException;
import com.zhousb.deviceManager.mapper.KiClientMapper;
import com.zhousb.deviceManager.mapper.KiFloorMapper;
import com.zhousb.deviceManager.mapper.KiPositionMapper;
import com.zhousb.deviceManager.mapper.SysUserClientMapper;
import com.zhousb.deviceManager.model.entity.KiClient;
import com.zhousb.deviceManager.model.entity.KiFloor;
import com.zhousb.deviceManager.model.entity.KiPosition;
import com.zhousb.deviceManager.model.entity.SysUserClient;
import com.zhousb.deviceManager.model.vo.PositionItemVo;
import com.zhousb.deviceManager.service.PositionService;
import com.zhousb.deviceManager.utils.AuthUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PositionServiceImpl implements PositionService {
    @Resource
    private SysUserClientMapper sysUserClientMapper;
    @Resource
    private KiClientMapper kiClientMapper;
    @Resource
    private KiFloorMapper kiFloorMapper;
    @Resource
    private KiPositionMapper kiPositionMapper;
    @Resource
    private AuthUtils authUtils;

    @Override
    public List<PositionItemVo> positionTree() {
        Long userId = authUtils.getUser().getUserId();
        List<PositionItemVo> result = new ArrayList<>();
        List<SysUserClient> userClients = sysUserClientMapper.selectList(new LambdaQueryWrapper<SysUserClient>().eq(SysUserClient::getUserId, userId));
        userClients.forEach(userClient -> {
            PositionItemVo clientItem = new PositionItemVo();
            KiClient client = kiClientMapper.selectById(userClient.getClientId());
            clientItem.setLabel(client.getClientName());

            List<PositionItemVo> floorList = new ArrayList<>();
            List<KiFloor> floors = kiFloorMapper.selectList(new LambdaQueryWrapper<KiFloor>().eq(KiFloor::getClientId, client.getId()));
            floors.forEach(floor -> {
                PositionItemVo floorItem = new PositionItemVo();
                List<PositionItemVo> positionList = new ArrayList<>();
                List<KiPosition> positions = kiPositionMapper.selectList(new LambdaQueryWrapper<KiPosition>().eq(KiPosition::getFloorId, floor.getId()));
                positions.forEach(position -> {
                    PositionItemVo positionItem = new PositionItemVo();
                    positionItem.setLabel(position.getPosition());
                    positionList.add(positionItem);
                });
                floorItem.setLabel(floor.getFloor());
                floorItem.setChildren(positionList);
                floorList.add(floorItem);
            });
            clientItem.setChildren(floorList);
            result.add(clientItem);
        });
        return result;
    }

    @Override
    public List<KiPosition> listPosition(String clientId, String floorName) {
        LambdaQueryWrapper<KiFloor> floorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        floorLambdaQueryWrapper.eq(KiFloor::getClientId, clientId)
                .eq(KiFloor::getFloor, floorName);
        KiFloor kiFloor = kiFloorMapper.selectOne(floorLambdaQueryWrapper);
        if (kiFloor == null) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<KiPosition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KiPosition::getFloorId, kiFloor.getId());
        return kiPositionMapper.selectList(queryWrapper);
    }

    @Override
    public boolean add(String floorId, String position) {
        LambdaQueryWrapper<KiPosition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KiPosition::getFloorId, floorId)
                .eq(KiPosition::getPosition, position);
        KiPosition kiPosition = kiPositionMapper.selectOne(queryWrapper);
        if (kiPosition != null) {
            throw new CustomerException("库房名称【" + position + "】已存在");
        }
        kiPosition = new KiPosition();
        kiPosition.setFloorId(Long.valueOf(floorId));
        kiPosition.setPosition(position);
        kiPositionMapper.insert(kiPosition);
        return true;
    }

    @Override
    public boolean delete(String id) {
        return kiPositionMapper.deleteById(id) > 0;
    }
}
